home *** CD-ROM | disk | FTP | other *** search
/ 500 MB Nyheder Direkte fra Internet 2 / 500 MB nyheder direkte fra internet CD 2.iso / start / data / text / style.txt < prev    next >
Text File  |  1994-09-21  |  36KB  |  794 lines

  1.  
  2.          INVENTING A PROGRAM
  3.   First, decide on your ultimate goal. Be optimistic. Maybe you'd 
  4. like the computer to play the perfect game of chess? Or translate 
  5. every English sentence into French?
  6.  
  7.           Research the past
  8.   Chances are, whatever you want the computer to do, someone else 
  9. has thought of the same idea already, and written a program for 
  10. it.
  11.   Find out. Ask your friends. Ask the people in nearby schools, 
  12. computer stores, computer centers, companies, libraries, and 
  13. bookstores. Look through books and magazines. There are even 
  14. books that list what programs have been written. Ask the company 
  15. you bought your computer from.
  16.   Even if you don't find exactly the program you're looking for, 
  17. you may find one that's close enough to be okay, or that will 
  18. work with just a little fixing, or can serve as part of your 
  19. program, or will at least give you a clue as to where to begin. 
  20. In one of the textbooks or magazines, you'll probably find a 
  21. discussion of the problem you're trying to solve, and the pros 
  22. and cons of various solutions to it ___ some methods are faster 
  23. than others.
  24.   Remember: if you keep your head in the sand, and don't look at 
  25. what other people have done already, your programming effort may 
  26. turn out to be a mere exercise useless to the rest of the world.
  27.  
  28.               Simplify
  29.   All too often, programmers embark on huge projects and never 
  30. get them done. Once you have an idea of what's been done before, 
  31. and how hard your project seems to be, simplify it. Instead of 
  32. making the computer play a perfect game of chess, how about 
  33. settling for a game in which the computer plays unremarkably but 
  34. at least doesn't cheat? Instead of translating every English 
  35. sentence into French, how about translating just English colors? 
  36. (We wrote that program already.) In other words, pick a less 
  37. ambitious, more realistic goal, which if you achieve it, will 
  38. make you feel good and will be a steppingstone to your ultimate 
  39. goal.
  40.   Finding a bug in a program is like finding a needle in a 
  41. haystack: removing the needle is easier if the haystack is small 
  42. than if you wait until more hay's been piled on.
  43.  
  44.            Specify the I/O
  45.   Make your new, simple goal more precise. That's called 
  46. specification. One way to be specific is to draw a picture, 
  47. showing what your screen will look like if your program's running 
  48. successfully.
  49.   In that picture, find the lines typed by the computer. They 
  50. become the PRINT statements in your program. Find the lines typed 
  51. by the human: they become the INPUT statements. Now you can start 
  52. writing your program: write the PRINT and INPUT statements on 
  53. paper, with a pencil, and leave blank lines between them. You'll 
  54. fill in the blanks later.
  55.                                          Suppose you want the 
  56. computer to find the average of two numbers. Your picture will 
  57. look like this:
  58. RUN
  59. WHAT'S THE FIRST NUMBER? number
  60. WHAT'S THE SECOND NUMBER? number
  61. THE AVERAGE IS number
  62. Your program at this stage will be:
  63. 10 INPUT "WHAT'S THE FIRST NUMBER";A
  64. 20 INPUT "WHAT'S THE SECOND NUMBER";B
  65. etc.
  66. 100 PRINT "THE AVERAGE IS";C
  67. All you have left to do is figure out what the ``etc.'' is. 
  68. Here's the general method. . . . 
  69.  
  70.                                               Choose your statements
  71.                                          Suppose you didn't have 
  72. a computer. Then how would you get the answer?
  73.                                          Would you have to use a 
  74. mathematical formula? If so, put the formula into your program, 
  75. but remember that the left side of the equation must have just 
  76. one variable. For example, if you're trying to solve a problem 
  77. about right triangles, you might have to use the Pythagorean 
  78. formula A2+B2=C2; but the left side of the equation must have 
  79. just one variable, so your program must say A=SQR(C^2-B^2), or 
  80. B=SQR(C^2-A^2), or C=SQR(A^2+B^2), depending on whether you're 
  81. trying to compute A, B, or C.
  82.                                          Would you have to use a 
  83. memorized list, such as an English-French dictionary or the 
  84. population of each state or the weight of each chemical element? 
  85. If so, that list becomes your DATA, and you need to READ it. If 
  86. it would be helpful to have the data numbered, so the first piece 
  87. of data is called X(1), the next piece of data is called X(2), 
  88. etc., use the DIM statement.
  89.                                          Subscripts are 
  90. particularly useful if one long list of information will be 
  91. referred to several times in the program.
  92.                                          Does your reasoning 
  93. repeat? That means your program should have a loop. If you know 
  94. how many times to repeat, say FOR . . . NEXT. If you're not sure 
  95. how often, say GO TO. If the thing that's to be repeated isn't 
  96. repeated immediately, but only after several other things have 
  97. happened, call the repeated part a subroutine, put it at the end 
  98. of your program (followed by RETURN), and say GOSUB whenever you 
  99. want it done.
  100.                                          At some point in your 
  101. reasoning, do you have to make a decision? Do you have to choose 
  102. among several alternatives? The way to say ``choose'' is: IF . . 
  103. . THEN. If you want the computer to make the choice arbitrarily, 
  104. ``by chance'', rather than because of a reason, say: IF RND(2)=1 
  105. THEN.
  106.                                          Do you have to compare 
  107. two things? The way to say ``compare A with B'' is: IF A=B THEN.
  108.           Write pseudocode
  109.   Some English teachers say that before you write a paper, you 
  110. should make an outline. Some computer teachers give similar 
  111. advice about writing programs.
  112.   The ``outline'' can look like a program in which some of the 
  113. lines are written in plain English instead of computerese. For 
  114. example, one statement in your outline might be:
  115. 130 A = the average of the twelve values of X
  116. Such a statement, written in English instead of in computerese, 
  117. is called pseudocode. Later, when you fill in the details, expand 
  118. that pseudocode into the following:
  119. 130 S=O
  120. 131 FOR I = 1 TO 12
  121. 132     S=S+X(I)
  122. 133 NEXT
  123. 134 A=S/12
  124.  
  125.           Organize yourself
  126.   Keep the program's over-all organization simple. That will make 
  127. it easier for you to expand it and find bugs. Here is some 
  128. folklore, handed down from generation to generation of 
  129. programmers, that will simplify your organization. . . . 
  130.   Use top-down programming. That means write a one-sentence 
  131. description of your program; then expand that sentence to several 
  132. sentences; then expand each of those sentences to several more 
  133. sentences; and so on, until you can't expand any more. Then turn 
  134. each of thse new sentences into lines of program. Your program 
  135. will then be in the same order as the English sentences, and 
  136. therefore organized the same way as an English-speaking mind.
  137.   A variation is to use subroutines. That means writing the 
  138. essence of the program as a very short main routine; instead of 
  139. filling in the grubby details immediately, replace each piece of 
  140. grubbiness by the word GOSUB. After the main routine is written, 
  141. write each subroutine. Your program will be like a good book: 
  142. your main routine will move swiftly, and the annoying details 
  143. will be relegated to the appendices at the back; the appendices 
  144. are called subroutines. Keep each subroutine down to 50 lines; if 
  145. it starts getting longer and grubbier, replace each piece of 
  146. grubbiness by a GOSUB to another subroutine, written afterwards 
  147. and having higher line numbers.
  148.   Avoid GO TO. It's hard for a human to understand a program 
  149. that's a morass of GO TO statements. It's like trying to read a 
  150. book where each paragraph says to turn to a different page! When 
  151. you must say GO TO, try to go forward instead of backwards, and 
  152. not go too far.
  153.   Divide your program into modules. A module is a bunch of 
  154. consecutive lines forming a unit that cannot be ``punctured''; in 
  155. other words, there is no GO-TO-type statement outside the module 
  156. that sends the computer to the module's middle; the only way the 
  157. module can be activated is by starting with its top line. (If the 
  158. module's particularly nice, the only way it can be deactivated is 
  159. by arriving at its bottom line; in other words, there is no 
  160. GO-TO-type statement in the module's middle that sends the 
  161. computer outside the module.) If you used top-down programming, 
  162. each module probably corresponds to one sentence in your 
  163. program's description. Write that sentence at the top of the 
  164. module, and put an apostrophe to the left of it.
  165.                                                    Use variables
  166.                                          After you've written a 
  167. few lines of your program, you may find that your reasoning 
  168. ``almost repeats''; several lines bear a strong resemblance to 
  169. each other. You can't use GO TO or FOR . . . NEXT or GOSUB . . . 
  170. RETURN unless the lines repeat exactly. To make the repetition 
  171. complete, use a variable to represent the parts that are 
  172. different.
  173.                                          For example, suppose 
  174. your program contains these lines:
  175. 130 PRINT 29.3428+9.87627*SQR(5)
  176. 140 PRINT 29.3428+9.87627*SQR(7)
  177. 150 PRINT 29.3428+9.87627*SQR(9)
  178. 160 PRINT 29.3428+9.87627*SQR(11)
  179. 170 PRINT 29.3428+9.87627*SQR(13)
  180. 180 PRINT 29.3428+9.87627*SQR(15)
  181. 190 PRINT 29.3428+9.87627*SQR(17)
  182. 200 PRINT 29.3428+9.87627*SQR(19)
  183. 210 PRINT 29.3428+9.87627*SQR(21)
  184. Each of those lines says PRINT 29.3428+9.87627*SQR(a number). The 
  185. number keeps changing, so call it X. Lines 130-210 can be 
  186. replaced by:
  187. 130 FOR X= 5 TO 21 STEP 2
  188. 140     PRINT 29.3428+9.87627*SQR(X)
  189. 150 NEXT
  190.                                          Here's a harder example 
  191. to fix:
  192. 130 PRINT 29.3428+9.87627*SQR(5)
  193. 140 PRINT 29.3428+9.87627*SQR(97.3)
  194. 150 PRINT 29.3428+9.87627*SQR(8.62)
  195. 160 PRINT 29.3428+9.87627*SQR(.4)
  196. 170 PRINT 29.3428+9.87627*SQR(200)
  197. 180 PRINT 29.3428+9.87627*SQR(12)
  198. 190 PRINT 29.3428+9.87627*SQR(591)
  199. 200 PRINT 29.3428+9.87627*SQR(.2)
  200. 210 PRINT 29.2428+9.87627*SQR(100076)
  201. Again, let's use X. Those nine lines can be combined like this:
  202. 130 DATA 5,97.3,8.62,.4,200,12,591,.2,100076
  203. 140 FOR I = 1 TO 9
  204. 150     READ X
  205. 160     PRINT 29.3428+9.87627*SQR(X)
  206. 170 NEXT
  207.                                          This one's even tougher:
  208. 130 PRINT 29.3428+9.87627*SQR(A)
  209. 140 PRINT 29.3428+9.87627*SQR(B)
  210. 150 PRINT 29.3428+9.87627*SQR(C)
  211. 160 PRINT 29.3428+9.87627*SQR(D)
  212. 170 PRINT 29.3428+9.87627*SQR(E)
  213. 180 PRINT 29.3428+9.87627*SQR(F)
  214. 190 PRINT 29.3428+9.87627*SQR(G)
  215. 200 PRINT 29.3428+9.87627*SQR(H)
  216. 210 PRINT 29.3428+9.87627*SQR(I)
  217. Let's assume A, B, C, D, E, F, G, H, and I have been computed 
  218. earlier in the program. The trick to shortening those lines is to 
  219. change the names of the variables. Throughout the program, say 
  220. X(1) instead of A, say X(2) instead of B, say X(3) instead of C, 
  221. etc. Say DIM X(9) at the beginning of your program. Then lines 
  222. 130-210 can be written:
  223. 130 FOR I=1 TO 9
  224. 140     PRINT 29.3428+9.87627*SQR(X(I))
  225. 150 NEXT
  226.  
  227.  
  228.     MAKE IT EFFICIENT
  229.   Your program should be efficient. That means it should use as 
  230. little of the computer's time and memory as possible.
  231.   To use less of the computer's memory, make your DIMensions as 
  232. small as possible. Try writing the program without any arrays at 
  233. all; if that turns out to be terribly inconvenient, use the 
  234. smallest and fewest arrays possible.
  235.   To use less of the computer's time, avoid having the computer 
  236. do the same thing more than once.
  237.   These lines force the computer to compute SQR(8.2*N+7) three 
  238. times:
  239. 50 PRINT SQR(8.3*N+7)+2
  240. 60 PRINT SQR(8.3*N+7)/9.1
  241. 70 PRINT 5-SQR(8.3*N+7)
  242. You should change them to:
  243. 49 K=SQR(8.3*N+7)
  244. 50 PRINT K+2
  245. 60 PRINT K/9.1
  246. 70 PRINT 5-K
  247.   These lines force the computer to compute X^9+2 a hundred 
  248. times:
  249. 50 FOR I = 1 TO 100
  250. 60     PRINT (X^9+2)/I
  251. 70 NEXT
  252. You should change them to:
  253. 49 K=X^9+2
  254. 50 FOR I = 1 TO 100
  255. 60     PRINT K/I
  256. 70 NEXT
  257.   These lines force the computer to count to 100 twice:
  258. 50 S=0
  259. 60 FOR I = 1 TO 100
  260. 70     S=S+X(I)
  261. 80 NEXT
  262. 90 PRINT "THE SUM OF THE X'S IS";S
  263. 100 P=1
  264. 110 FOR I = 1 TO 100
  265. 120    P=P*X(I)
  266. 130 NEXT
  267. 140 PRINT "THE PRODUCT OF THE X'S IS";P
  268. You should change them to:
  269. 50 S=0
  270. 51 P=1
  271. 60 FOR I = 1 TO 100
  272. 70     S=S+X(I)
  273. 71     P=P*X(I)
  274. 80 NEXT
  275. 90 PRINT "THE SUM OF THE X'S IS";S
  276. 140 PRINT "THE PRODUCT OF THE X'S IS";P
  277.   Here are more tricks for making your program run faster. . . . 
  278.   Instead of exponents, use multiplication:
  279. Slow          Faster
  280. 50 Y=X^2      50 Y=X*X
  281.  
  282.                              Combine statements, to form a single 
  283. line:
  284. Slow                                     Faster
  285. 50 A=3                                   50 A=3: B=7
  286. 60 B=7
  287. Warning: an IF statement cannot be combined with a later 
  288. statement.
  289. Cannot be combined
  290. 50 IF I<1 THEN GO TO 200
  291. 60 B=7
  292.                              If a number contains a decimal point 
  293. and is in a loop, turn the number into a variable:
  294. Slow                                     Faster
  295.                                          49 C=3.1
  296. 50 FOR I = 1 TO 900                      50 FOR I = 1 TO 900
  297. 60     S=S+3.1/I                         60     S=S+C/I
  298. 70 NEXT                                  70 NEXT
  299.                              Omit the variable after NEXT (unless 
  300. the FOR...NEXT loop contains another FOR...NEXT loop):
  301. Slow                                     Faster
  302. 50 NEXT I                                50 NEXT
  303.                              If your program doesn't involve 
  304. decimals or large numbers, put this statement at the beginning of 
  305. your program:
  306. 1 DEFINT A-Z
  307. If your program involves just a few decimals or large numbers, 
  308. begin your program by saying DEFINT A-Z, and put an exclamation 
  309. point after every variable that stands for a real number.
  310.  
  311.                                              Alphabetizing
  312.                              Suppose you want the computer to 
  313. alphabetize a list of names. What's the best strategy?
  314.                              Imagine trying to alphabetize the 
  315. list yourself ___ each name is written on a file card, and you 
  316. have to put the deck of cards in alphabetical order.
  317.                              One strategy would be to compare the 
  318. second card with the first, and swap them if necessary; then look 
  319. at the third card, and swap if necessary; and so on to the end of 
  320. the deck. A different strategy would be to put all the A's in one 
  321. pile, all the B's in another, etc., and then sort each pile.
  322.                              Which strategy is better? If the 
  323. file has ten cards or less, the swap method is faster; if the 
  324. file is very long, the 26-pile method is faster but requires 
  325. space to lay out 26 piles.
  326.                              Which method would make a more 
  327. efficient program? That depends on how long the file is and 
  328. whether your computer lacks fast parts or large memory.
  329.  
  330.                                              Prime numbers
  331.                              An integer is called composite if 
  332. it's the product of two other integers. 35 is composite, because 
  333. it's 5*7; 9 is composite, because it's 3*3; 12 is composite, 
  334. because it's 2*6; 13 is not composite, and is therefore called 
  335. prime. This program tells whether a number is prime or composite:
  336. 10 INPUT "WHAT'S YOUR FAVORITE POSITIVE INTEGER";N
  337. 20 FOR I = 1 TO N-1
  338. 30     FOR J = 1 TO N-1
  339. 40         IF N=I*J THEN PRINT N;"IS";I;"TIMES";J;"AND 
  340. COMPOSITE": END
  341. 50     NEXT
  342. 60 NEXT
  343. 70 PRINT N;"IS PRIME"
  344. Line 10 waits for you to type a number N. Line 40 checks whether 
  345. N is the product of two other integers; if it is, the computer 
  346. says N is composite.
  347.   How efficient is that program? Since it contains no arrays, it 
  348. doesn't require much space in the memory. But if N turns out to 
  349. be prime, line 40 is encountered once for every I and once for 
  350. every J; altogether it's encountered (N-1)2 times. If N is a 
  351. large number, around a million, (N-1)2 is around a trillion. To 
  352. do line 40 a trillion times will take a typical microcomputer 
  353. many years. In fact, if you say that your favorite number is 
  354. 999983 (which is close to a million), the typical microcomputer 
  355. will take about 200 years before it comes to the conclusion that 
  356. your number is prime! By the time the program finishes running, 
  357. you'll be dead and so will your children! The program's very 
  358. inefficient.
  359.   Some small improvements are possible; for example, I and J can 
  360. start at 2 instead of 1. But so long as you have a loop inside a 
  361. loop, the time will remain very large.
  362.   The following strategy requires just one loop: divide N by 
  363. every integer less than it, to see whether the quotient is ever 
  364. an integer. Here's the program:
  365. 10 INPUT "WHAT'S YOUR FAVORITE POSITIVE NUMBER";N
  366. 20 FOR I = 2 TO N-1
  367. 40     Q=N/I: IF Q=INT(Q) THEN PRINT N;"IS";I;"TIMES";Q;"AND 
  368. COMPOSITE":END
  369. 60 NEXT
  370. 70 PRINT N;"IS PRIME"
  371. Line 40 consists of two parts. The first part says to divide N by 
  372. an integer (I); the quotient's called Q. The second part says 
  373. that if Q is an integer, N is composite.
  374.   How efficient is our new program? If N turns out to be prime, 
  375. line 40 is encountered once for every I; altogether it's 
  376. encountered N-2 times. That's less than in the previous program, 
  377. where it was encountered (N-1)2 times. If N is about a million, 
  378. our new program is nearly a million times faster than the 
  379. previous one! To determine whether 999983 is prime, the new 
  380. program takes a typical microcomputer about 3 hours instead of 
  381. 200 years.
  382.   We can improve the program even further. If an N can't be 
  383. divided by 2, it can't be divided by any even number; so after 
  384. checking divisibility by 2, we have to check divisibility by just 
  385. 3, 5, 7, . . . , N-2. Let's put that short-cut into our program, 
  386. and also say that every N less than 4 is prime:
  387. 10 INPUT "WHAT'S YOUR FAVORITE NUMBER";N
  388. 11 IF N<4 THEN PRINT N;"IS PRIME":END                                 
  389. 12 Q=N/2: IF Q=INT(Q) THEN PRINT N;"IS 2 TIMES";Q;"AND 
  390. COMPOSITE": END
  391. 20 FOR I = 3 TO N-2 STEP 2
  392. 40     Q=N/I: IF Q=INT(Q) THEN PRINT N;"IS";I;"TIMES";Q;"AND 
  393. COMPOSITE": END
  394. 60 NEXT
  395. 70 PRINT N;"IS PRIME"
  396. Line 12 checks divisibility by 2; lines 20-60 check divisibility 
  397. by 3, 5, 7, . . . , N-2. If N is prime, line 40 is encountered 
  398. N/2 - 2 times, which is about half as often as in the previous 
  399. program; so our new program takes about half as long to run. On a 
  400. typical microcomputer, it takes about 1½ hours to handle 999983.
  401.   Our goal was to find a pair of integers whose product is N. If 
  402. there is such a pair of integers, the smaller one will be no more 
  403. than the square root of N, so we can restrict our hunt to the 
  404. integers not exceeding the square root of N:
  405. 10 INPUT "WHAT'S YOUR FAVORITE NUMBER";N
  406. 11 IF N<4 THEN PRINT N;"IS PRIME":END
  407. 12 Q=N/2: IF Q=INT(Q) THEN PRINT N;"IS 2 TIMES";Q;"AND 
  408. COMPOSITE": END
  409. 20 FOR I = 3 TO SQR(N)*1.00001 STEP 2
  410. 40     Q=N/I: IF Q=INT(Q) THEN PRINT N;"IS";I;"TIMES";Q;"AND 
  411. COMPOSITE": END
  412. 60 NEXT
  413. 70 PRINT N;"IS PRIME"
  414. The ``1.00001'' is to give a margin of safety, in case the 
  415. computer rounds SQR(N) a bit down. If N is near a million, line 
  416. 40 is encountered about 500 times, which is much less than the 
  417. 500,000 times encountered in the previous program and the 
  418. 1,000,000,000,000 times in the original. This program lets the 
  419. typical microcomputer handle 999983 in about 6 seconds. That's 
  420. much quicker than the earlier versions, which required 1½ hours, 
  421. or 3 hours, or 200 years!
  422.   Moral: a few small changes in a program can make the computer 
  423. take 6 seconds instead of 200 years.
  424.   The frightening thing about this example is that the first 
  425. version we had was so terrible, but the only way to significantly 
  426. improve it was to take a totally fresh approach. To be a 
  427. successful programmer, you must always keep your mind open, and 
  428. hunt for fresh ideas.
  429.  
  430.  
  431.                  DON'T BE SILLY
  432.   After you've written a program, skim through it to see whether 
  433. any of its lines are silly. Eliminate the silly lines, so that 
  434. your program becomes briefer, simpler, and more pleasant.
  435.   In the following examples, I assume your program is numbered 
  436. 10, 20, 30, 40, . . . 
  437.  
  438.  Don't tell the computer to GO TO the next line
  439.   For example, don't say:
  440. 30 GO TO 40
  441. Omit it. The computer will go to line 40 anyway.
  442.   Here's another example of a GO TO that goes to the next line:
  443. 30 IF X<7 THEN GO TO 40
  444. Omit it. The computer will go to line 40 anyway.
  445.   Here's another example of a GO TO that goes to the next line:
  446. 30 IF X<7 THEN PRINT "WOW" ELSE GO TO 40
  447. Omit the ``ELSE GO TO 40''; just say:
  448. 30 IF X<7 THEN PRINT "WOW"
  449.  
  450.  Don't write an IF that skips over just one line
  451.   For example, don't write:
  452. 30 IF X<7 THEN GO TO 50
  453. 40 PRINT "WOW"
  454. That line 30 is an IF that skips over just line 40. It's silly! 
  455. Combine lines 30 and 40 into a single line:
  456. 30 IF X>=7 THEN PRINT "WOW"
  457.  
  458.    Don't write an IF followed by its opposite
  459.   For example, don't write:
  460. 30 IF X<7 THEN PRINT "GEE"
  461. 40 IF X>=7 THEN PRINT "WOW"
  462. Combine them into a single line:
  463. 30 IF X<7 THEN PRINT "GEE" ELSE PRINT "WOW"
  464. (That combination works only on computers that understand the 
  465. word ELSE.)
  466.   Here's another example of an IF followed by its opposite:
  467. 30 IF X<7 THEN GO TO 100
  468. 40 IF X>=7 THEN PRINT "WOW"
  469. Remove the IF from line 40:
  470. 30 IF X<7 THEN GO TO 100
  471. 40 PRINT "WOW"
  472. The new version does the same thing as the original, because the 
  473. computer reaches the new 40 only if X is not less than 7.
  474.                                                      Here's 
  475. another IF followed by its opposite:
  476. 30 IF X<7 THEN GO TO 50
  477. 40 IF X>=7 THEN PRINT "WOW"
  478. Applying the same technique as before, remove the IF from line 
  479. 40:
  480. 30 IF X<7 THEN GO TO 50
  481. 40 PRINT "WOW"
  482. But also notice that line 30 is an IF that skips over just one 
  483. line; we've uncovered another piece of silliness! Applying more 
  484. cosmetics, we get down to a single line:
  485. 30 IF X>=7 THEN PRINT "WOW"
  486.                                                      Here's 
  487. another IF followed by its opposites:
  488. 30 IF X<7 THEN GO TO 100
  489. 40 IF X=7 THEN GO TO 200
  490. 50 IF X>7 THEN PRINT "WOW"
  491. Remove the last IF:
  492. 30 IF X<7 THEN GO TO 100
  493. 40 IF X=7 THEN GO TO 200
  494. 50 PRINT "WOW"
  495.  
  496.  
  497.        AVOID ROUND-OFF ERRORS
  498.   The computer cannot handle decimals accurately. If you say 
  499. X=.1, the computer can't set X equal to .1 exactly; instead, it 
  500. will set X equal to a number very, very close to .1. The reason 
  501. for the slight inaccuracy is that the computer thinks in 
  502. ``binary'', not decimals; and .1 cannot be expressed in binary 
  503. exactly.
  504.   Usually you won't see the slight inaccuracy: when you ask the 
  505. computer to PRINT a number, the computer prints it rounded to six 
  506. significant figures, and the inaccuracy is so small it doesn't 
  507. show up in the rounded result. But there are three situations in 
  508. which the inaccuracy can be noticed:
  509.   1. Telling the computer to do A-B, where A is almost equal to 
  510. B, and the first several digits of A are the same as the first 
  511. several digits of B. For example, if you ask a typical 
  512. microcomputer to print 8.001-8, the computer will not print .001; 
  513. instead it will print .000999451. The same thing happens if you 
  514. do 8.001+(-8).
  515.   If you ask the typical microcomputer to print 987654.1-987654, 
  516. it will print .125 instead of .1. The error can get magnified: if 
  517. you ask the computer to multiply 987654.1-987654 by 1000, it will 
  518. print .125*1000, which is 125, instead of .1*1000, which is 100. 
  519. If you ask it to find the reciprocal of 987654.1-987654, it will 
  520. print 1/.125, which is 8, instead of 1/.1, which is 10.
  521.   Those are the errors produced by a typical microcomputer. The 
  522. errors produced by your computer might be slightly more or less. 
  523. But even if your computer is a maxi that costs $10,000,000, it 
  524. makes those same kinds of errors.
  525.   2. Saying ``FOR X = A TO B STEP C'', where C is a decimal and 
  526. the loop will be done many times. For example:
  527. 10 FOR X = 1 TO 2 STEP .1
  528. 20     PRINT X
  529. 30 NEXT
  530. Theoretically, the computer should print 1, 1.1, 1.2, 1.3, 1.4, 
  531. 1.5, 1.6, 1.7, 1.8, 1.9, and 2. But that's not what actually 
  532. happens. In line 10, the computer can't handle the decimal .1 
  533. accurately. The last few numbers the typical microcomputer thinks 
  534. of are:
  535. slightly more than 1.7
  536. slightly more than 1.8
  537. slightly more than 1.9
  538. The computer does not think of the next number, slightly more 
  539. than 2.0, because line 10 says not to go past 2. In line 20, the 
  540. word PRINT makes the computer print the numbers rounded to six 
  541. significant digits, so it prints:
  542.  1
  543.  1.1
  544.  1.2
  545.  1.3
  546.  1.4
  547.  1.5
  548.  1.6
  549.  1.7
  550.  1.8
  551.  1.9
  552. It does not print 2.
  553.   If you want to compute 1 + 1.1 + 1.2 + 1.3 + 1.4 + 1.5 + 1.6 + 
  554. 1.7 + 1.8 + 1.9 + 2, you might be tempted to write this program:
  555. 5 S=0
  556. 10 FOR X = 1 TO 2 STEP .1
  557. 20     S=S+X
  558. 30 NEXT
  559. 40 PRINT S
  560. The computer will print a reasonable-looking answer: 14.5. But 
  561. that ``answer'' is wrong, because the last number it added was 
  562. slightly more than 1.9; it never added 2. The correct answer is 
  563. 16.5.
  564.                                          One remedy is to change 
  565. line 10 to this:
  566. 10 FOR X = 1 TO 2.05 STEP .1
  567. The .05 after the 2 allows for the margin of error. The general 
  568. strategy is to change ___ 
  569. 10 FOR X = A TO B STEP C
  570. to this:
  571. 10 FOR X = A TO B+C/2 STEP C
  572.                                          An alternative remedy is 
  573. to replace ___ 
  574. 10 FOR X = 1 TO 2 STEP .1
  575. by this pair of lines:
  576. 10 FOR I = 10 TO 20
  577. 11     X= I/10
  578. As I goes from 10 to 20, X will go from 1 to 2 in steps of .1. 
  579. This remedy is the most accurate of all, since it eliminates 
  580. decimals from line 10. But the division in line 11 makes the 
  581. program very slow.
  582.                                          3. Asking the computer 
  583. whether two numbers X and Y are equal. It's unwise to ask whether 
  584. X is exactly equal to Y, since both X and Y have probably been 
  585. affected by some slight error. Instead, ask the computer whether 
  586. the difference between X and Y is much tinier than Y:
  587. Bad                                              Good
  588. IF X=Y THEN                                      IF 
  589. ABS(X-Y)<=.000001*ABS(Y) THEN
  590. The .000001 is requesting that the first six significant digits 
  591. of X be the same as the first six significant digits of Y (except 
  592. that the sixth significant digit might be off by one).
  593.  
  594.                                                     Why binary?
  595.                                          From those discussions, 
  596. you might think computers should be made differently, and that 
  597. they should use the decimal system instead of binary. There are 
  598. two counterarguments.
  599.                                          First, binary arithmetic 
  600. is faster.
  601.                                          Second, even if 
  602. computers were using the decimal system, inaccuracy would still 
  603. occur. To store the fraction 2/3 accurately by using the decimal 
  604. system, the computer would have to store a decimal point followed 
  605. by infinitely many 6's. That would require an infinite amount of 
  606. space in memory, which is impossible (unless you know how to 
  607. build an infinitely large computer?) So even in the decimal 
  608. system, some fractions must be approximated instead of handled 
  609. exactly.
  610.  
  611.                                                 Begin with the tiny
  612.                                          According to 
  613. mathematicians, addition is supposed to obey these laws:
  614. A+0 is exactly the same as A
  615. A+B is exactly the same as B+A
  616. A+-A is exactly the same as 0
  617. (A+B)+C is exactly the same as A+(B+C)
  618.                                          On the computer, the 
  619. first three laws hold, but the last does not. If A is a decimal 
  620. tinier than C, the computer does (A+B)+C more accurately than 
  621. A+(B+C). So to add a list of numbers accurately, begin by adding 
  622. together the tiniest decimals in the list.
  623.  
  624.     TEST YOUR PROGRAM
  625.   When you've written a program, test it: type RUN and see 
  626. whether it works.
  627.   If the computer does not gripe, your tendency will be to say 
  628. ``Whoopee!'' Don't cheer too loudly. The answers the computer is 
  629. printing may be wrong. Even if its answers look reasonable, don't 
  630. assume they're right: the computer's errors can be very subtle. 
  631. Check some of its answers by doing them with a pencil.
  632.   Even if the answers the computer prints are correct, don't 
  633. cheer. Maybe you were just lucky. Type different input, and see 
  634. whether your program still works. Chances are, there's something 
  635. you can input that will make your program go crazy or print a 
  636. wrong answer. Your mission: to find input that will reveal the 
  637. existence of a bug.
  638.   Try six kinds of input. . . . 
  639.  
  640.     Try simple input
  641.   Type in simple integers, like 2 and 10, so the computation is 
  642. simple, and you can check the computer's answers easily.
  643.  
  644. Try input that increases
  645.   See how the computer's answer changes when the input changes 
  646. from 2 to 1000.
  647.   Does the change in the computer's answer look reasonable? Does 
  648. the computer's answer go up when it should go up, and down when 
  649. it should go down? . . . and by a reasonable amount?
  650.  
  651. Try input testing each IF
  652.   For a program that says ___ 
  653. 30 IF X<7 THEN GO TO 100
  654. input an X less than 7 (to see whether line 100 works), then an X 
  655. greater than 7 (to see whether line 40 works), then an X equal to 
  656. 7 (to see whether you really want ``<'' instead of ``<=''), then 
  657. an X very close to 7, to check round-off error.
  658.   For a program that says ___ 
  659. 30 IF A^2+B<C THEN GO TO 100
  660. input an A, B, and C that make A^2+B less than C. Then try inputs 
  661. that make A^2+B very close to C.
  662.  
  663.     Try extreme input
  664.   What happens if you input:
  665. a huge number, like 45392000000 or 1E35?
  666. a tiny number, like .00000003954 or 1E-35?
  667. a trivial number, like 0 or 1?
  668. a typical number, like 45.13?
  669. a negative number, like -52?
  670. Find out.
  671.                              If the input is supposed to be a 
  672. string, what happens if you input AAAAA or ZZZZZ? If there are 
  673. supposed to be two inputs, what happens if you input the same 
  674. thing for each?
  675.  
  676.                                 Try input making some lines act strange
  677.                              If your program contains division, 
  678. try input that will make the divisor be zero or a tiny decimal 
  679. close to zero. If your program contains the square root of a 
  680. quantity, try input that will make the quantity be negative. If 
  681. your program says ``FOR I=A TO B'', try input that will make B be 
  682. less than A, then equal to A. If your program mentions X(I), try 
  683. input that will make I be zero or negative or greater than the 
  684. DIM.
  685.                              Try input that causes round-off 
  686. error: for a program that says ``A-B'' or says ``IF A=B THEN'', 
  687. try input that will make A almost equal B.
  688.  
  689.                                               Try garbage
  690.                              Many people hate computers because 
  691. they often print wrong answers. A computer can print a wrong 
  692. answer because its machinery is broken, or because a program has 
  693. a bug. But the main reason why computers print wrong answers is 
  694. that the input is incorrect. Incorrect input is called garbage. 
  695. It has several causes. . . . 
  696.                              The user's finger slips Instead of 
  697. 400, he inputs 4000. Instead of 27, he inputs 72. Trying to type 
  698. .753, he leaves out the decimal point.
  699.                              The user got wrong information He 
  700. tries to input the temperature, but his thermometer is leaking. 
  701. He tries to input the results of a questionnaire, but everybody 
  702. who filled out his questionnaires lied.
  703.                              The instructions aren't clear The 
  704. program asks HOW FAR DID THE BALL FALL, and the user doesn't know 
  705. whether to type the distance in feet or in meters.
  706.                              Is time to be given in seconds or 
  707. minutes? Are angles to be measured in degrees or radians?
  708.                              If the program asks WHAT IS YOUR 
  709. NAME, should the user type JOE SMITH or ``SMITH,JOE'' or just 
  710. JOE?
  711.                              Can the user input Y instead of YES?
  712.                              Maybe the user isn't clear about 
  713. whether to insert commas, quotation marks, and periods. If 
  714. several items are to be typed, should they be typed on the same 
  715. line or on separate lines? If your program asks HOW MANY BROTHERS 
  716. AND SISTERS DO YOU HAVE, and the user has 2 brothers and 3 
  717. sisters, should he type 5 or ``2,3'' or ``2 BROTHERS AND 3 
  718. SISTERS''?
  719.                              For a quiz that asks WHO WAS THE 
  720. FIRST U.S. PRESIDENT, what if the user answers GEORGE WASHINGTON 
  721. or simply WASHINGTON or G. WASHINGTON or GENERAL GEORGE 
  722. WASHINGTON or PRESIDENT WASHINGTON or MARTHA'S HUSBAND? Make the 
  723. instructions clearer:
  724. WHO WAS THE FIRST U.S. PRESIDENT (GIVE JUST HIS LAST NAME)?
  725.                              The user is trying to be funny or 
  726. sabotage the computer Instead of inputting his name, he types an 
  727. obscene comment. When asked how many brothers and sisters he has, 
  728. he says 275.
  729.                              Responsibility It's your 
  730. responsibility as a programmer to make sure that the directions 
  731. for using your program are clear, and that the program rejects 
  732. ridiculous input.
  733.                              For example, if your program is 
  734. supposed to print weekly paychecks, it should refuse to print 
  735. checks for more than $10000. Your program should contain these 
  736. lines:
  737. 20 INPUT "HOW MUCH MONEY DID THE EMPLOYEE EARN";E
  738. 30 IF E>10000 THEN PRINT E;"IS QUITE A BIG PAYCHECK! I DON'T 
  739. BELIEVE YOU.": PRIN T "PLEASE RETYPE YOUR REQUEST.": GO TO 20
  740. Line 30 is called an error trap (or an error-handling routine). 
  741. Your program should contain several, to prevent printing checks 
  742. that are too small (2¢?) or negative or otherwise ridiculous 
  743. ($200.73145?)
  744.                              To see how your program reacts to 
  745. input that's either garbage or unusual, ask the person sitting 
  746. next to you to run your program. That person might input 
  747. something you never thought of.
  748.  
  749.                    DOCUMENT IT
  750.   Write an explanation that helps other people understand your 
  751. program. An explanation is called documentation; when you write 
  752. an explanation, you're documenting the program.
  753.   You can write the documentation on a separate sheet of paper 
  754. (to be put in the computer center's library), or you can make the 
  755. computer print the documentation when the user types RUN or LIST.
  756.   A popular device is to begin the RUN by making the computer ask 
  757. the user:
  758. DO YOU NEED INSTRUCTIONS?
  759.   You need two kinds of documentation: how to use the program, 
  760. and how the program was written.
  761.  
  762.              How to use the program
  763.   Your explanation of how to use the program should include:
  764. the program's name
  765. how to get the program from the disk
  766. the program's purpose
  767. a list of other programs that must be combined with this program, 
  768. to make a workable combination
  769. the correct way to type the input and data (show an example)
  770. the correct way to interpret the output
  771. the program's limitations (input it can't handle, a list of error 
  772. messages that might be printed, round-off error)
  773. a list of bugs you haven't fixed yet
  774.  
  775.            How the program was written
  776.   An explanation of how you wrote the program will help other 
  777. programmers borrow your ideas, and help them expand your program 
  778. to meet new situations. It should include:
  779. your name
  780. the date you finished it
  781. the computer you wrote it for
  782. the language you wrote it in (probably BASIC)
  783. the name of the method you used (``solves quadratic equations by 
  784. using the quadratic formula'')
  785. the name of the book or magazine where you found the method
  786. the name of any program you borrowed ideas from
  787. an informal explanation of how program works (``It loops until 
  788. A>B, then computes the weather forecast.'')
  789. the purpose of each module
  790. the meaning of each variable
  791. the meaning of reaching a line (if the program says IF X<60 THEN 
  792. GO TO 1000, say ``Reaching line 1000 means the student 
  793. flunked.'')
  794.